package com.tool4j.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tool4j.entity.TermInfo;
import com.tool4j.mapper.TermInfoMapper;
import com.tool4j.service.TermInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class TermInfoServiceImpl extends ServiceImpl<TermInfoMapper, TermInfo> implements TermInfoService {

    @Autowired
    private TermInfoMapper termInfoMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBatch(Long userId, List<TermInfo> list) {
        termInfoMapper.delete(null);
        for (TermInfo item : list) {
            item.setUserId(userId);
        }
        termInfoMapper.saveBatch(list);
    }

    @Override
    public List<TermInfo> findAll(Long userId) {
        LambdaQueryWrapper<TermInfo> query = Wrappers.lambdaQuery(TermInfo.class)
                .eq(TermInfo::getUserId, userId);
        return termInfoMapper.selectList(query);
    }
}
